//
//  ArrangementHelper.m
//  We_Travel
//
//  Created by lanou3g on 15/12/16.
//  Copyright © 2015年 lanou3g. All rights reserved.
//

#import "ArrangementHelper.h"
#import "NH_DataDownloadTools.h"
#import "url.h"
#import "NH_ViewModel.h"
#import "Country.h"
#import "CoreDataToolHelper.h"

@implementation ArrangementHelper
//单例
+ (instancetype)shareArrangementHelper{
    static ArrangementHelper  *arrangementHelper = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        arrangementHelper = [[ArrangementHelper alloc]init];
        [arrangementHelper requireData];
    });
    return arrangementHelper;
}
//请求数据
- (void)requireData{
    [NH_DataDownloadTools downloadDataWithURL:NHUrl andMethod:@"get" andBody:nil andBlock:^(NSData *data) {
        NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        NSMutableArray *arr = dictionary[@"localities"];
        for (NSDictionary *dic in arr) {
            NH_ViewModel *model = [[NH_ViewModel alloc]init];
            model.id_View = (NSInteger)dic[@"id"];
            [model setValuesForKeysWithDictionary:dic];
            [self.travelArray addObject:model];
        }
        if (self.resultBlock) {
            self.resultBlock();
        }
    }];
}
//懒加载
- (NSMutableArray *)travelArray{
    if (!_travelArray) {
        _travelArray = [NSMutableArray array];
    }
    return _travelArray;
}
//获取到已经选择的行程(NH_ViewModel)
- (NSMutableArray *)selectdTravelArray{
    NSMutableArray *array = [NSMutableArray array];
    for (Country *country in  [[CoreDataToolHelper shareCoreDataTool]fetchCountry]) {
        for (NH_ViewModel *model in self.travelArray) {
            if ([country.countryName isEqualToString:model.name]) {
                [array addObject:model];
            }
        }
}
    return array;
}
@end
